﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ObjectFeatures;
using System.Threading;
using System.Globalization;
using System.Windows.Markup;
using System.IO;
using System.Diagnostics;

namespace WpfAlarmTimer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Worker _worker;
        private bool _running = true;
        private Thread _refresher;

        public MainWindow()
        {
            this._worker = new Worker();
            this.DataContext = this._worker;
            this._worker.AddAlarm(new Alarm(new DateTime(2014, 1, 1)));
            this._worker.AddAlarm(new Alarm(new DateTime(2015, 1, 1)));
            this._worker.StartTimer(new ObjectFeatures.Timer(3676));
            this._worker.StartTimer(new ObjectFeatures.Timer(30));
            this._refresher = new Thread(Refresh);
            this._refresher.Name = "Refresher Thread";
            InitializeComponent();
            this._worker.Start();
            this._refresher.Start();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this._running = false;
            this._worker.Stop();
            if (this._refresher.IsAlive) this._refresher.Interrupt();
        }

        private void Refresh() {
            while (_running)
            {
                try
                { 
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        this.timeTextBlock.Text = GetFormatedTime();
                        this.dateTextBlock.Text = GetFormatedDate();
                    }), null);
                    
                    Thread.Sleep(1000); 
                }
                catch (ThreadInterruptedException) { }
            }
        }

        private string GetFormatedTime()
        {
            return DateTime.Now.ToString("HH:mm:ss");
        }

        private string GetFormatedDate()
        {
            return DateTime.Now.ToString("dd-MM-yyyy");
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.PreviousSize.Width > 0)
            {
                ResizeTextBlock(this.dateTextBlock, (e.NewSize.Width / e.PreviousSize.Width));
                ResizeTextBlock(this.timeTextBlock, (e.NewSize.Width / e.PreviousSize.Width));
            }
        }

        private void ResizeTextBlock(TextBlock tb, double ratio) {
            string toDisplay = tb.Text;
            double actualFontSize = tb.FontSize;
            double desiredWidth = tb.ActualWidth;
            Typeface typeface = new Typeface(tb.FontFamily, tb.FontStyle, tb.FontWeight, tb.FontStretch);
            FormattedText ft = new FormattedText(toDisplay, CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, typeface, actualFontSize, tb.Foreground);
            double actualWidth = ft.Width;
            double newSize = actualFontSize * ratio;
            ft = new FormattedText(toDisplay, CultureInfo.InvariantCulture, System.Windows.FlowDirection.LeftToRight, typeface, newSize, tb.Foreground);
            if (ft.Width >= desiredWidth) newSize -= 1;
            tb.FontSize = newSize;
            tb.Height = ft.Height + 2;
        }

        private void addAlarmBtn_Click(object sender, RoutedEventArgs e)
        {
            //TODO display dialog to create an alarm
            //this._worker.AddAlarm(a);
        }

        private void addTimerBtn_Click(object sender, RoutedEventArgs e)
        {
            ObjectFeatures.Timer t = new ObjectFeatures.Timer(60);
            TimerEditDialogResult result = TimerEditDialog.Show(t);
            if (result == TimerEditDialogResult.Save)
            {
                this._worker.StartTimer(t);
            }
        }
    }
}
